package com.waimai.mobile.ui.fragment;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.waimai.mobile.R;
import com.waimai.mobile.api.WaiMaiServer;
import com.waimai.mobile.api.entity.Comment;
import com.waimai.mobile.api.entity.ReceiveAddress;
import com.waimai.mobile.api.model.ResponseModel;
import com.waimai.mobile.ui.activity.RestaurantActivity;
import com.waimai.mobile.ui.adapter.AddressItemAdapter;
import com.waimai.mobile.ui.adapter.CommentItemAdpater;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.CheckedChange;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.api.BackgroundExecutor;

import java.util.Iterator;
import java.util.List;

/**
 * Created by lin on 2015/5/29.
 */

@EFragment(R.layout.fragment_restaurant_comment)
public class RestaurantCommentFragment extends Fragment {


    public int commentLevel = Comment.COMMENT_ALL;
    public boolean onlyGetCommentsWithContent;

    @RestService
    public WaiMaiServer server;

    @Bean
    public CommentItemAdpater adapter;

    @ViewById
    public TextView mentionMsgTv;

    @ViewById
    public ListView commentListLv;

    @AfterViews
    public void afterViews(){
        commentLevel = Comment.COMMENT_ALL;
        onlyGetCommentsWithContent = true;
        loadData();
    }

    public void loadData(){
        loading();
        loadCommentList(((RestaurantActivity)getActivity()).restaurantId, commentLevel);
    }

    @CheckedChange(R.id.seeCommentWithContentCBox)
    public void getCommentsWithContent(CompoundButton hello, boolean isChecked){
        BackgroundExecutor.cancelAll("loadCommentList", true);
        onlyGetCommentsWithContent = isChecked;
        loadData();
    }


    @CheckedChange(R.id.allCommentsRBtn)
    public void getAllComments(CompoundButton hello, boolean isChecked){
        if(!isChecked){
            return ;
        }
        BackgroundExecutor.cancelAll("loadCommentList", true);
        commentLevel = Comment.COMMENT_ALL;
        loading();
        loadCommentList(((RestaurantActivity)getActivity()).restaurantId, commentLevel);
    }

    @CheckedChange(R.id.goodCommentsRBtn)
    public void getGoodComments(CompoundButton hello, boolean isChecked){
        if(!isChecked){
            return ;
        }
        BackgroundExecutor.cancelAll("loadCommentList", true);
        commentLevel = Comment.COMMENT_GOOD;
        loading();
        loadCommentList(((RestaurantActivity)getActivity()).restaurantId, commentLevel);
    }

    @CheckedChange(R.id.middleCommentsRBtn)
    public void getMiddleComments(CompoundButton hello, boolean isChecked){
        if(!isChecked){
            return ;
        }
        BackgroundExecutor.cancelAll("loadCommentList", true);
        commentLevel = Comment.COMMENT_MIDDLE;
        loading();
        loadCommentList(((RestaurantActivity)getActivity()).restaurantId, commentLevel);
    }

    @CheckedChange(R.id.badCommentsRBtn)
    public void getBadComments(CompoundButton hello, boolean isChecked){
        if(!isChecked){
            return ;
        }
        BackgroundExecutor.cancelAll("loadCommentList", true);
        commentLevel = Comment.COMMENT_BAD;
        loading();
        loadCommentList(((RestaurantActivity)getActivity()).restaurantId, commentLevel);
    }

    @Background(id="loadCommentList")
    public void loadCommentList(Integer restaurantId, int commentLevel){
        try {
            ResponseModel<List<Comment>> model = server.comments(restaurantId, commentLevel);
            if(model != null){
                switch (model.getStatus().getStatusCode()) {
                    case ResponseModel.STATUS_GET_DATA_SUCCESS:
                        if(onlyGetCommentsWithContent){
                            for(Iterator<Comment> it = model.getData().iterator(); it.hasNext();){
                                Comment comment = it.next();
                                if(comment.getContent() == null || "".equals(comment.getContent())){
                                    it.remove();
                                }
                            }
                        }
                        if(model.getData().size() == 0){
                            loadedNoneData();
                        }else {
                            loadedSuccess(model.getData());
                        }
                        break;
                    case ResponseModel.STATUS_GET_DATA_NONE:
                        loadedNoneData();
                        break;
                    case ResponseModel.STATUS_SERVER_ERROR:
                        serverError();
                        break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            netError();
        }
    }

    @UiThread
    public void loading(){
        commentListLv.setVisibility(View.GONE);
        mentionMsgTv.setText("正在加载...");
        mentionMsgTv.setVisibility(View.VISIBLE);
        mentionMsgTv.setClickable(false);
    }

    @UiThread
    public void loadedSuccess(List<Comment> comments){
        adapter.setComments(comments);
        commentListLv.setAdapter(adapter);
        commentListLv.setVisibility(View.VISIBLE);
        mentionMsgTv.setVisibility(View.GONE);
    }

    @UiThread
    public void loadedNoneData(){
        mentionMsgTv.setText("抱歉，没有相应的评价哦...");
    }

    @UiThread
    public void serverError(){
        mentionMsgTv.setText("服务器错误，点击重试");
        mentionMsgTv.setClickable(true);
    }

    @UiThread
    public void netError(){
        mentionMsgTv.setText("网络错误，点击重试");
        mentionMsgTv.setClickable(true);
    }

}